/*

  Trapezoidal Method 
 Author: vishwajeet Dusane
*/

#include<stdio.h>
#include<iostream.h>
class TrapezoidalMethod
{
  private:
       double  subInterval[100],function[100] ; 
       double piValue,h;
 public: 
 void divideIntervals(int);
 void geth(int,int,int);
 void getFunction(int);  
 void calculate(int);
 int checkAccuracy();

};


void TrapezoidalMethod::divideIntervals(int intervals)
 {
 
  for(int i=0;i<=intervals;i++)
   {
    subInterval[i]=(double)i/intervals;       	
  //  cout<<subInterval[i]<<"\n";
   }         
 }
void TrapezoidalMethod::geth(int a,int b,int intervals)
{
   double h;

   h =(double) (b-a)/intervals;
  // cout<<"h="<<h;
  // getchar();
   
}
void TrapezoidalMethod::getFunction(int interval)
{
 double x;
   for(int i=0;i<=interval;i++)
  {

    x=subInterval[i];   
    //cout<<"subInterval="<<x;
    function[i]=(double)(1/(1+x*x));              
    //cout<<"function["<<i<<"]"<<"="<<function[i];
   // getchar();       
  }

} 

void TrapezoidalMethod::calculate(int interval)
{

     //Trapezoidal Formula        
     
  double temp,subTemp,temp1;

     temp=(double)(function[0]+function[interval-1]);
       
//    cout<<"1st temp\n"<<temp;    

    for(int i=1;i<interval;i++)
    {
          subTemp=(double)subTemp+function[i];
    }   
   subTemp=(double)2*subTemp;
  // cout<<"subTemp="<<subTemp;

   piValue=(double)temp+subTemp;
   //cout<<"1st piValue ="<<piValue;
   
   piValue=(double)piValue*0.5; 
//   cout<<"Iteration :"<<piValue;                
   piValue=(double)h*piValue;
   cout<<"pi="<<piValue/4;

   getchar();
}

int TrapezoidalMethod::checkAccuracy()
{
    if((piValue-3.1312)>0.0001)
      return 0;
    else
      return 1;
}



int main() 
{
  
 int interval=2,accuracy,choice;
 TrapezoidalMethod Trapezoidal; 
  cout<<"(1/(1+x^2))";
  cout<<"Enter Ur Choice:";
  cin>>choice;

  if(choice==1)
  {
  while(1)
   {  
    Trapezoidal.divideIntervals(interval);    
    Trapezoidal.geth(0,1,interval);
    Trapezoidal.getFunction(interval);
    Trapezoidal.calculate(interval);  
    accuracy= Trapezoidal.checkAccuracy();
     if(accuracy==0)
      break;
    interval=interval+2;
   }   
  } 
}


